package com.fenghongzhang.sample.extendmethod

import java.lang.StringBuilder

/**
 * 扩展函数.
 * 在原因的类的基础上,增加方法.
 *
 * 类名和可以随便起,尽量和要添加的类同名
 *
 */
fun String.countletters(): Int{
    var i = 0
    for (char in this){
        if(char.isLetter()){
            i++
        }
    }
    return i
}

fun String.toUpper():String{
    val  stringBuilder:StringBuilder = StringBuilder()
    for (char in this){
        if(char.isLetter()){
            val toUpperCase = char.toUpperCase()
            stringBuilder.append(toUpperCase)
            continue
        }
        stringBuilder.append(char)
    }
    return stringBuilder.toString()

}

operator fun String.plus(string: String):String{
    var stringBuilder = StringBuilder()
    stringBuilder.append(string)
    return stringBuilder.toString()
}

operator fun String.times(n : Int):String {
    val string = StringBuilder()
    repeat(n){
        string.append(this)
    }
    return string.toString()
}